///////////////////////////////////////////////////////////////////////
//  CS 3716 (Winter 2012), Assignment #4                       		 //
//  Program File Name: RecListManager.java                    		 //
//       Student Name: Jonathan Hart-Wall, Adam Murphy         		 //
//					   Yifu Liang, Joshua Dowling			   		 //
//         Login Name: jcwhw1, agm264 , n96yl, n23jdd                //
//              MUN #: 200933455, 200933133, 200565679, 200703684    //
///////////////////////////////////////////////////////////////////////

import java.io.*;
import java.util.ArrayList;

public class RecListManager{

	/**
	 * RecListManager Parameters
	 */
	private File unloadedRecListDir;
	private File loadedRecListDir;
	private String unloadedFileLocation;
	private ArrayList<RecList> recList;
	
	/**
	 * RecListManager constructor
	 * @param loadedFileLocation
	 * @param unloadedFileLocation
	 */
	public RecListManager(String loadedFileLocation, String unloadedFileLocation){
		unloadedRecListDir = new File(unloadedFileLocation);
		loadedRecListDir = new File(loadedFileLocation);
		recList = new ArrayList<RecList>();
	}
	
	/**
	 * Accessor method
	 * @return recList
	 */
	public ArrayList<RecList> getRecLists(){
		return recList;
	}
	
	/**
	 * Accesor method to return specific recList
	 * @param name
	 * @return recL
	 * @throws NoSuchListException
	 */
	public RecList getRecList(String name) throws NoSuchListException{
		for (RecList recL : recList){
			if (recL.getName().equals(name))
				return recL;
		}
		throw new NoSuchListException();
	}
	
	/**
	 * load method that loads all previous RecLists from external textfile.
	 * @throws IOException
	 * @throws FileNotFoundException
	 */
	public void load() throws IOException, FileNotFoundException{
		File[] loadedRecListFiles = loadedRecListDir.listFiles();
			for (File recListFile : loadedRecListFiles){
				BufferedReader br = new BufferedReader(new FileReader(recListFile));
			
				String listName = br.readLine();
				String verifier = br.readLine();
				if (verifier.equals("null"))
					verifier = null;
				String line = br.readLine();
	
				ArrayList<MovieRec> movieRecs = new ArrayList<MovieRec>();
				while (!(line = br.readLine()).trim().equals("")){
					String[] movieRecParams = line.split("#");
					movieRecs.add(new MovieRec(movieRecParams[0], Double.parseDouble(movieRecParams[3]), Integer.parseInt(movieRecParams[1]), movieRecParams[2], movieRecParams[4]));
				}
			
				ArrayList<String> history = new ArrayList<String>();
				while ((line = br.readLine()) != null){
					history.add(line.trim());
				}
			
				recList.add(new RecList(listName, movieRecs, history, verifier));
				br.close();
			}		
	}
	
	/**
	 * load method that loads a specific recList with String filename and String listName.
	 * @param fileName
	 * @param listName
	 * @param editor
	 * @throws ListNameTakenException
	 * @throws IOException
	 * @throws FileNotFoundException
	 */
	public void load(String fileName, String listName, String editor) throws ListNameTakenException, IOException, FileNotFoundException
	{
		File[] unloadedRecListFiles = unloadedRecListDir.listFiles();
		File fileToBeLoaded = null;
		for (File unloadedRecListFile : unloadedRecListFiles)
		{
			if (unloadedRecListFile.getName().equals(fileName))
			{
				fileToBeLoaded = unloadedRecListFile;
				break;
			}
		}
		
		if (fileToBeLoaded == null)
			throw new FileNotFoundException();
			
		for (RecList list : recList)
		{
			if (list.getName().equals(listName))
				throw new ListNameTakenException();
		}
		
		ArrayList<MovieRec> movieRecs = new ArrayList<MovieRec>();	
		BufferedReader br = new BufferedReader(new FileReader(fileToBeLoaded));
		String line = "";
			
		while ((line = br.readLine()) != null)
		{
			if (!line.trim().equals(""))
			{
				String[] movieRecParams = line.split("#");
				movieRecs.add(new MovieRec(movieRecParams[0], Double.parseDouble(movieRecParams[3]), Integer.parseInt(movieRecParams[1]), movieRecParams[2], editor));
			}
		}
		ArrayList<String> history = new ArrayList<String>();
		
		recList.add(new RecList(listName, movieRecs, history, null));		
	}
}